package com.dongdongshop.front.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.cart.pojo.Cart;
import com.dongdongshop.cart.pojo.TbOrderItem;
import com.dongdongshop.cart.service.CartService;
import com.dongdongshop.cart.service.OrderService;
import com.dongdongshop.front.pojo.AddressVo;
import com.dongdongshop.front.pojo.TbUser;
import com.dongdongshop.front.service.AddressService;
import com.dongdongshop_util.Result;
import com.dongdongshop_util.ResultEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("getOrderInfo")
public class GetOrderInfoController {

    private Logger logger= LoggerFactory.getLogger(GetOrderInfoController.class);

    //调用地址表
    @Reference(check = false,timeout = 50000)
    private AddressService addressService;

    //调用购物车
    @Reference(check = false,timeout = 50000)
    private CartService cartService;

    @Reference(check = false,timeout = 50000)
    private OrderService orderService;

    //跳转到结算页
    @RequestMapping("toGetOrderInfo")
    public String toGetOrderInfo(Model model){
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        TbUser user = (TbUser) subject.getPrincipal();
        String username = user.getUsername();
        List<AddressVo> addressVo = addressService.getAddressByUsername(username);
        model.addAttribute("addressVo",addressVo);
        //根据用户名获取购物车信息
        List<Cart> cartList = cartService.getCartByRedis(username);
        //总价格
        double totalFees = 0;
        //获取商品件数，循环几次，就是几件
        int num = 0;
        for (Cart cart : cartList) {
            //从redis获取购物车数据
            List<TbOrderItem> orderItemList = cart.getOrderItemList();
            for (TbOrderItem orderItem : orderItemList) {
                num ++;
                BigDecimal totalFee = orderItem.getTotalFee();
                totalFees += totalFee.doubleValue();
            }
        }
        model.addAttribute("totalFees",totalFees);
        model.addAttribute("num",num);
        model.addAttribute("cartList",cartList);
        return "admin/getOrderInfo";
    }
    @ResponseBody
    @RequestMapping("getByAddressId")
    public Result getByAddressId(Long id){
        try {
            //根据收货人id 查询收货人地区名称（省市县）
            AddressVo addressVo = addressService.getAddressVoByAddressId(id);
            return Result.success(ResultEnum.SUCCESS,addressVo);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //生成订单
    @ResponseBody
    @RequestMapping("addOrder")
    public Result addOrder(Long id,String paymentType){
        try {
            //获取当前登录的用户
            Subject subject = SecurityUtils.getSubject();
            TbUser user = (TbUser) subject.getPrincipal();
            String username = user.getUsername();
            //根据收货人id，查询收货人地区名称（省市县）
            AddressVo addressVo = addressService.getAddressVoByAddressId(id);
            //生成订单
            orderService.addOrder(username,addressVo,paymentType);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //跳转到微信支付页面
    @RequestMapping("toPay")
    public String toPay(String payment,Model model){
        model.addAttribute("payment",payment);
        return "admin/pay";
    }
}
